package com.diandian.backend.eleme.retail.exchange.service;

import com.diandian.backend.eleme.retail.exchange.req.SkuCategoryPropertyListReq;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.eleme.retail.boot.starter.dto.ShopListDTO;
import com.diandian.eleme.retail.boot.starter.dto.SkuCategoryPropertyListDTO;
import com.diandian.eleme.retail.boot.starter.response.*;
import com.diandian.eleme.retail.boot.starter.service.ElemaMerchantShopManageService;
import com.diandian.eleme.retail.boot.starter.service.ElemaSkuCategoryManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 饿了吗 - 商品 - 连锁商品库类目同步
 */
@Service
public class BusinessSkuCategoryManageService {

    @Autowired
    private ElemaSkuCategoryManageService skuCategoryManageService;

    @Autowired
    private ElemaMerchantShopManageService elemaMerchantShopManageService;

    public ElemaResponseSkuCategoryPropertyList skuCategoryPropertyList(String source, String secret, SkuCategoryPropertyListReq data) throws Exception {

        // 随机获取一个门店编码，用来查询动态类目数据
        List<ElemaResponseShopListData> allStoreCodeList = elemaMerchantShopManageService.shopList(source, secret, new ShopListDTO()).getData();
        if (allStoreCodeList == null || allStoreCodeList.size() == 0) {
            throw new DefaultException(1, "查询类目属性值列表异常：线上门店列表为空");
        }
        String shopId = allStoreCodeList.get(0).getShop_id();

        // 请求API，查询数据
        SkuCategoryPropertyListDTO dto = new SkuCategoryPropertyListDTO();
        dto.setCat3Id(data.getCat3Id());
        dto.setShopId(shopId);
        return skuCategoryManageService.skuCategoryPropertyList(source, secret, dto);
    }

}
